package com.sky.task;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.ArrayList;

/**
 * @author yangeit
 * @date 2023年11月30日 11:03
 * @Description
 */
@Slf4j
public class ExcelDemo {

    public static void main(String[] args) throws Exception {
//        readExcel("D://exceldemo.xlsx");

//        easyExcelRead();

        easyExcelWrite();
    }

    private static void easyExcelWrite() {

        //准备假数据
        ArrayList<XXUser> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            XXUser demoData2 = new XXUser();
            demoData2.setCity("长沙"+i);
            demoData2.setId(i+1+"");
            demoData2.setName("yangeit"+i);

            list.add(demoData2);
        }



        EasyExcel.write("D://exceldemo3.xlsx",XXUser.class).sheet("sheet1").doWrite(list);





    }

    private static void easyExcelRead() {


        EasyExcel.read("D://exceldemo.xlsx", XXUser.class, new ReadListener<XXUser>() {
            @Override
            public void invoke(XXUser data, AnalysisContext context) {
                log.info("解析到一条数据：{}",data);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                log.info("所有数据解析完毕");
            }
        }).sheet().doRead();




    }


    private static void readExcel(String s) throws Exception {

        FileInputStream fileInputStream = new FileInputStream(new File(s));

        //        1. 在内存中创建文件对象 ：1
        XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);

//        workbook.getSheet("book")
        XSSFSheet sheetAt = workbook.getSheetAt(0);

//获取最后一行的索引
        int lastRowNum = sheetAt.getLastRowNum();

        for (int i = 0; i <= lastRowNum; i++) {

            XSSFRow row = sheetAt.getRow(i);

            XSSFCell cell0 = row.getCell(0);
            XSSFCell cell1 = row.getCell(1);
            XSSFCell cell2 = row.getCell(2);

            cell0.setCellType(CellType.STRING);



            String stringCellValue0 = cell0.getStringCellValue();
            String stringCellValue1 = cell1.getStringCellValue();
            String stringCellValue2 = cell2.getStringCellValue();

            System.out.println(stringCellValue0+"   "+stringCellValue1+"   "+stringCellValue2);


        }
        workbook.close();
        fileInputStream.close();


    }

    private static void exportExcel() throws IOException {
        //        1. 在内存中创建文件对象 ：1
        XSSFWorkbook workbook = new XSSFWorkbook();

//        2. 创建Sheet页
        XSSFSheet book = workbook.createSheet("book");
//        3. 创建行
        XSSFRow row = book.createRow(0);

//        4.创建单元格
        row.createCell(0).setCellValue("序号");
        row.createCell(1).setCellValue("姓名");
        row.createCell(2).setCellValue("城市");


        //        3. 创建行
        XSSFRow row1 = book.createRow(1);

//        4.创建单元格
        row1.createCell(0).setCellValue("1");
        row1.createCell(1).setCellValue("韩立");
        row1.createCell(2).setCellValue("黄枫谷");


        //        3. 创建行
        XSSFRow row2 = book.createRow(2);

//        4.创建单元格
        row2.createCell(0).setCellValue("2");
        row2.createCell(1).setCellValue("南宫婉");
        row2.createCell(2).setCellValue("天鹰教");

        FileOutputStream fileOutputStream = new FileOutputStream(new File("D://exceldemo.xlsx"));
        workbook.write(fileOutputStream);

        fileOutputStream.flush();
        fileOutputStream.close();
        workbook.close();
    }
}
